package com.haust.community.utils;

/**
 * @Auther: csp1999
 * @Date: 2020/11/27/20:34
 * @Description: Redis Key的工具类
 */
public class RedisKeyUtil {

    private static final String SPLIT = ":";
    private static final String PREFIX_ENTITY_LIKE = "like:entity";
    private static final String PREFIX_USER_LIKE = "like:user";
    private static final String PREFIX_FOLLOWEE = "followee";// 关注某人
    private static final String PREFIX_FOLLOWER = "follower";// 某人关注我
    private static final String PREFIX_KAPTCHA = "kaptcha";// 验证码key的前缀
    private static final String PREFIX_TICKET = "ticket";// 登录凭证key的前缀
    private static final String PREFIX_USER = "user";// 登录用户key的前缀
    private static final String PREFIX_UV = "uv";// 独立访客(通过用户IP地址排重统计)
    private static final String PREFIX_DAU = "dau";// 日活跃用户(通过ID排重统计)
    private static final String PREFIX_POST = "post";// 帖子key前缀

    /**
     * 获取单日UV集合(HyperLogLog)的key
     * @param date
     * @return
     */
    public static String getUVKey(String date) {
        return PREFIX_UV + SPLIT + date;
    }

    /**
     * 获取区间UV(两个日期之间统计的UV)集合(HyperLogLog)的key
     * @param startDate
     * @param endDate
     * @return
     */
    public static String getUVKey(String startDate, String endDate) {
        return PREFIX_UV + SPLIT + startDate + SPLIT + endDate;
    }

    /**
     * 获取单日活跃用户集合(Bitmap)的key
     * @param date
     * @return
     */
    public static String getDAUKey(String date) {
        return PREFIX_DAU + SPLIT + date;
    }

    /**
     * 获取区间活跃用户(两个日期之间统计的活跃用户)集合(Bitmap)的key
     * @param startDate
     * @param endDate
     * @return
     */
    public static String getDAUKey(String startDate, String endDate) {
        return PREFIX_DAU + SPLIT + startDate + SPLIT + endDate;
    }

    /**
     * 某个实体赞的key
     * <p>
     * key ---> like:entity:entityType:entityId -> set(userId)
     *
     * @param entityType
     * @param entityId
     * @return
     */
    public static String getEntityLikeKey(int entityType, int entityId) {
        return PREFIX_ENTITY_LIKE + SPLIT + entityType + SPLIT + entityId;
    }

    /**
     * 某个用户累计的赞的key
     * <p>
     * key ---> like:user:userId -> int
     *
     * @param userId
     * @return
     */
    public static String getUserLikeKey(int userId) {
        return PREFIX_USER_LIKE + SPLIT + userId;
    }

    /**
     * 某个用户关注某个实体的集合key
     * <p>
     * key
     * ---> followee:userId:entityType
     * ---> zset(entityId,now),zset为有序集合,以now作为排序分数
     * now表示当前时间的时间数,可以根据时间大小排序
     *
     * @param userId
     * @param entityType
     * @return
     */
    public static String getFolloweeKey(int userId, int entityType) {
        return PREFIX_FOLLOWEE + SPLIT + userId + SPLIT + entityType;
    }

    /**
     * 某个实体拥有的粉丝集合key
     * <p>
     * key ---> follower:entityType:entityId -> zset(userId,now)
     *
     * @param entityType
     * @param entityId
     * @return
     */
    public static String getFollowerKey(int entityType, int entityId) {
        return PREFIX_FOLLOWER + SPLIT + entityType + SPLIT + entityId;
    }

    /**
     * 登录验证码的key
     *
     * @param owner 验证码所属者
     * @return
     */
    public static String getKaptchaKey(String owner) {
        return PREFIX_KAPTCHA + SPLIT + owner;
    }

    /**
     * 登录的凭证的key
     *
     * @param ticket
     * @return
     */
    public static String getTicketKey(String ticket) {
        return PREFIX_TICKET + SPLIT + ticket;
    }

    /**
     * 用户的key
     *
     * @param userId
     * @return
     */
    public static String getUserKey(int userId) {
        return PREFIX_USER + SPLIT + userId;
    }

    /**
     * 获取帖子分数集合的key
     * @return
     */
    public static String getPostScoreKey() {
        return PREFIX_POST + SPLIT + "score";
    }
}
